********************************************************************************
** Table A8: Merger Effect Estimates on Clinical Quality With and Without 
**  	     Severity Adjustment 
** 
********************************************************************************



****
**** Settings 
****
* survival and readmission data 
local fpath_survreadm_dat = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/survreadm/input"

* crosswalks
local fpath_crosswalks = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/build/crosswalks"

* build folder 
local fpath_build_output = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/build/output"

* output directory 
local fpath_output = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/output_20230606/"

** which sections of code to run?
local prepare_data = 1
local run_regressions = 1
local combine_tables = 1
local clean_tables = 1


if `prepare_data' == 1 {
	
	* this is a id-year-cohort panel constructed in 04_stack_unadj_survreadm.R
	insheet using "`fpath_survreadm_dat'/stackedv2_unadj_meas.csv", clear

	** drop immediately divested hospitals from the control group 
	preserve 
	import excel "`fpath_crosswalks'/Acq AHA Xwalk (Dalton).xlsx", sheet("combined (stata input)") 	firstrow clear
	keep if (divested_yr <= 2008) | (divested_yr == 2015 & target2 == 1)
	rename id_aha_dalton id
	keep id 
	tempfile drop_divested
	save `drop_divested', replace
	restore 

	cap drop _merge 
	merge m:1 id using `drop_divested', keep(master) nogen



	* collapse to hosp-yeargroup-measure level
	* the yeargroups are 2004-06, 08-10, 12-14
	gen acq_legacy = (ind12_14acq_legacy > 0) | (ind08_11acq_legacy > 0) | (ind04_06acq_legacy > 0)
	gen target = (ind12_14target > 0) | (ind08_11target > 0) | (ind04_06target> 0)
	gen target2 = (ind12_14target2 > 0) | (ind08_11target2 > 0) | (ind04_06target2 > 0)
	gen acq_other = (ind12_14acq_other > 0) | (ind08_11acq_other > 0) | (ind04_06acq_other > 0)
	keep if inlist(year, 2006, 2010, 2014)

	*  restrict the sample: Legacy, Target, Other forprofits 
	keep if forprofit == 1 
	drop if target2 == 1 | acq_other == 1 

	label var ind08_11acq_legacy "Acquirer * 08-11"
	label var ind12_14target "Target * 12-14"

	* define post-treatment 
	gen postm = ( year >= 2008)
	gen postm_target = postm * target
	gen postm_legacy = postm * acq_legacy 
	label var postm_target "Post 2008 * Target"
	label var postm_legacy "Post 2008 * Acquirer"

	* check
	assert target2 + acq_other == 0 

	* run DD as before but with hospital-measure FEs, yeargroup-measure FEs 
	* gen hospital-measure FEs
	egen hospmeasure = group(id cohort)

	*gen year-measure FEs
	egen yrmeasure = group(year cohort)

	* gen HRR-yeargroup-measure FEs
	egen hrryrmeasure = group(hrrcode year cohort)

	* gen HRR year time TREND by cohort 
	gen hrr_time_ami = hrrcode * year * (cohort == "ami")
	gen hrr_time_chf = hrrcode * year * (cohort == "chf")
	gen hrr_time_hip = hrrcode * year * (cohort == "hip")
	gen hrr_time_pnu = hrrcode * year * (cohort == "pnu")
	gen hrr_time_stk = hrrcode * year * (cohort == "stk")

	* interact log(beds) with measure
	tab cohort, gen(cr)
	foreach v of varlist cr* {
		gen lbds`v' = log(beds_tot) * `v'
		local lab: variable label `v'
		local lab = subinstr("`lab'", "cohort==", "log(beds) * ", 1)
		label var lbds`v' "`lab'"

	}

	egen id2 = group(id)
}

** run the regressions
if `run_regressions' == 1 {

foreach v in shdx rhdx snra rnra  {


	************************************************************************
	* survival and readmission regression - main
	************************************************************************
	reghdfe `v' postm_legacy postm_target if forprofit == 1, absorb(hospmeasure yrmeasure) vce(cluster id2)

	regsave using "`fpath_output'/`v'_main_tablea8", p autoid addlabel(rhs, "`v'", spec, "main") replace 

}
}



*******
******* combine tables 
*******
if `combine_tables' == 1 {
	clear
	save "`fpath_output'/main_unadj_survreadm_comb_tablea8.dta", emptyok replace  	
	

	foreach v in shdx rhdx snra rnra  {
	 
		clear 
		
		foreach spec in "main" {
			
			
			di"`v'" 
			use "`fpath_output'/`v'_`spec'_tablea8", clear 
	
			append using "`fpath_output'/main_unadj_survreadm_comb_tablea8.dta"
		
			save "`fpath_output'/main_unadj_survreadm_comb_tablea8.dta", replace 		
		}
	
	}

}




******* 
******* clean up the tables and save to excel 
******* 
if `clean_tables' == 1 {
	

	use "`fpath_output'/main_unadj_survreadm_comb_tablea8.dta", clear
	drop if strpos(var, "_cons") | strpos(var, "interim") | strpos(var, "hrr")

	replace var = strtitle(subinstr(var, "postm_", "", 1))
	replace var = "Acquirer" if var == "Legacy"
	replace var = "Target" if var == "Target"

	cap drop _id
	cap drop r2 spec 
	foreach v in coef stderr pval {
	replace `v' = round(`v', .00001)
	}

	gen asterisk = "***" if pval <= .01
	replace asterisk = "**" if pval > .01 & pval <= .05
	replace asterisk = "*" if pval >.05 & pval < .1
	gen paren1 = "("
	gen paren2 = ")"

	egen coef2 = concat(coef asterisk)
	egen stderr2 = concat(paren1 stderr paren2)
	
	drop coef stderr paren1 paren2 asterisk pval
	rename coef2 coef
	rename stderr2 stderr 
	
	reshape wide coef stderr , i(rhs N) j(var) string
	order N, last 

	rename rhs depvar
	
	rename coefAcquirer post_Acquirer
	rename coefTarget post_Target
	rename stderrAcquirer se_Acquirer
	rename stderrTarget se_Target



	replace depvar = "Discordant vendor count" if depvar == 	"discvendcount"
	replace depvar = "Log of costs per inpatient" if depvar == 	"ladjcosts_w"
	replace depvar = "Log of revenue per inpatient" if depvar == 	"ladjrev_w"
	replace depvar = "Log of price index" if depvar == "ldafny_price05"
	replace depvar = "Log of FTE" if depvar == "logfte"
	replace depvar = "Log of 1 + capital investment" if depvar == 	"lw_capinv_tot"	
	replace depvar = "Profit margin" if depvar == "profit_margin_w"
	replace depvar = "Readmission" if depvar == "rhdx"
	replace depvar = "Survival" if depvar == "shdx"
	replace depvar = "Readmission unadjusted" if depvar == "rnra"
	replace depvar = "Survival unadjusted" if depvar == "snra"
	replace depvar = "Physician churn rate" if depvar == "wcost_churn"
	replace depvar = "Physician entry rate" if depvar == "wcost_entrants"
	replace depvar = "Physician exit rate" if depvar == "wcost_exits"

	gen order = 1 if depvar == "Discordant vendor count"
	replace order = 2 if depvar == "Physician entry rate"
	replace order = 3 if depvar == "Physician exit rate" 
	replace order = 4 if depvar == "Physician churn rate" 
	replace order = 5 if depvar == "Log of FTE" 
	replace order = 6 if depvar == "Log of 1 + capital investment" 
	replace order = 7 if depvar == "Log of costs per inpatient"
	replace order = 8 if depvar == "Log of price index" 
	replace order = 9 if depvar == "Log of revenue per inpatient" 
	replace order = 10 if depvar == "Profit margin" 
	replace order = 11 if depvar == "Survival" 
	replace order = 12 if depvar == "Readmission" 
	sort order 

	drop order 

	export excel using "`fpath_output'/TableA8.xls", sheet("Unadjusted_survreadm", replace) firstrow(varlabel)


}

















